import { message } from "antd"

const tools = {
    // 图片下载方法
    downloadImg: (imgsrc, name) => {
        function downloadBlob(url) {
            return new Promise((resolve, reject) => {
                var xhr = new XMLHttpRequest()
                xhr.open('GET', url)
                xhr.responseType = 'blob'
                xhr.onload = function () {
                    if (xhr.status === 200) {
                        resolve(xhr.response)
                    } else {
                        reject(new Error(xhr.statusText || '图片下载失败'))
                    }
                }
                xhr.onerror = function () {
                    reject(new Error('图片下载失败'))
                }
                xhr.send() 
            })
        }

        function myBrowser() {
            var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
            if (userAgent.indexOf("OPR") > -1) {
                return "Opera";
            } //判断是否Opera浏览器 OPR/43.0.2442.991
            if (userAgent.indexOf("Firefox") > -1) {
                return "FF";
            } //判断是否Firefox浏览器  Firefox/51.0
            if (userAgent.indexOf("Trident") > -1) {
                return "IE";
            } //判断是否IE浏览器  Trident/7.0; rv:11.0
            if (userAgent.indexOf("Edge") > -1) {
                return "Edge";
            } //判断是否Edge浏览器  Edge/14.14393
            if (userAgent.indexOf("Chrome") > -1) {
                return "Chrome";
            } // Chrome/56.0.2924.87
            if (userAgent.indexOf("Safari") > -1) {
                return "Safari";
            } //判断是否Safari浏览器 AppleWebKit/534.57.2 Version/5.1.7 Safari/534.57.2
        }
        downloadBlob(imgsrc).then(data => {
            // const type = data.type.split('/')[1]
            if (myBrowser() == "IE") {
                // window.navigator.msSaveBlob(data, `${name}.${type}`);
                window.navigator.msSaveBlob(data, name);
            } else {
                var a = document.createElement("a");
                // a.download = `${name}.${type}`;
                a.download = name;
                a.href = URL.createObjectURL(data);
                a.click();
            }
        }).catch(err => {
            message.error(err)
        })
        // let image = new Image();
        // // 解决跨域 Canvas 污染问题
        // image.setAttribute("crossOrigin", "anonymous");
        // image.onload = function() {
        // let canvas = document.createElement("canvas");
        // canvas.width = image.width;
        // canvas.height = image.height;
        // let context = canvas.getContext("2d");
        // context.drawImage(image, 0, 0, image.width, image.height);
        // let url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
        // let a = document.createElement("a"); // 生成一个a元素
        // let event = new MouseEvent("click"); // 创建一个单击事件
        // a.download = name || "picture"; // 设置图片名称
        // a.href = url; // 将生成的URL设置为a.href属性
        // a.dispatchEvent(event); // 触发a的单击事件
        // };
        // image.src = imgsrc;
    },

    // 获取当天时间
    todayDate: () => {
        let nowDate = new Date()
        let year = nowDate.getFullYear();     //获取当前时间的年份
        let month = nowDate.getMonth() + 1;   //获取当前时间的月份，月份从0开始，所以需要加一
        let day = nowDate.getDate();          //获取当前时间的日期
        let hour = nowDate.getHours();        //获取当前时间的小时数
        let minute = nowDate.getMinutes();    //获取当前时间的分钟数
        let second = nowDate.getSeconds();    //获取当前时间的秒数

        let newMonth = month < 10 ? '0' + month : month
        let newDay = day < 10 ? '0' + day : day
        let newHour = hour < 10 ? '0' + hour : hour
        let newMinute = minute < 10 ? '0' + minute : minute
        let newSecond = second < 10 ? '0' + second : second
        const time = year + '-' + newMonth + '-' + newDay + ' ' + newHour + ':' + newMinute + ':' + newSecond
        return time
    }

}

export default tools